import Feature from 'ol/Feature'
import { isArray, coordinateUtils } from '../utils/index'
import { v4 as uuidv4 } from 'uuid'
/**
 * 绘制直线
 * @param {Array} lineArr
 * @returns {Object}
 */
export const lineString = (lineArr) => {
  if (!isArray(lineArr) || !lineArr.length) {
    throw Error('绘制lineString时参数lineArr格式错误')
  }
  const ids = []
  const features = lineArr.map((item) => {
    const {
      id = uuidv4(),
      name = '',
      data,
      type = 'line',
      distance,
      showDistance
    } = item || {}
    ids.push(id)

    if (!isArray(data) || !data.length) {
      throw Error('绘制lineString：传入参数data格式错误，必须为经纬度数组')
    }
    const [prev, next] = data
    const geometry = new LineString(data)
    // 如果需要显示距离，并且distance字段没有值，计算距离
    if (showDistance === true && !distance) {
      const dis = coordinateUtils.getWGS84Distance(prev, next)
      item.distance = `${parseInt(dis)}m`
    }
    return new Feature({
      id,
      name,
      type: 'lineString',
      property: { ...item },
      lineType: type === 'line' ? 'line' : 'dash',
      geometry
    })

  })
  return {
    features,
    ids
  }
}